//
//  FeedCell.m
//  TableViewCellCustomize
//
//  Created by Kishikawa Katsumi on 10/05/05.
//  Copyright 2010 Kishikawa Katsumi. All rights reserved.
//

#import "FeedCell.h"

@interface FeedCellContentView : UIView {
    FeedCell *cell;
    BOOL highlighted;
}

@end

@implementation FeedCellContentView

- (id)initWithFrame:(CGRect)frame cell:(FeedCell *)tableCell {
    if (self = [super initWithFrame:frame]) {
        cell = tableCell;
        self.opaque = YES;
        self.backgroundColor = cell.backgroundColor;
    }
    
    return self;
}

- (void)dealloc {
    [super dealloc];
}

- (void)drawRect:(CGRect)rect {
    [cell.icon drawAtPoint:CGPointMake(4.0f, 3.0f)];
    
    highlighted ? 
    [[UIColor whiteColor] set] : 
    [[UIColor colorWithWhite:0.300 alpha:1.000] set];
    [cell.title drawInRect:
     CGRectMake(26.0f, 
                1.0f, 
                self.frame.size.width - 30.0f, 40.0f) 
                  withFont:[UIFont fontWithName:@"HelveticaNeue-Bold" 
                                           size:16.0f] 
             lineBreakMode:UILineBreakModeTailTruncation];
}

- (void)setHighlighted:(BOOL)b {
    highlighted = b;
    [self setNeedsDisplay];
}

- (BOOL)isHighlighted {
    return highlighted;
}

@end

@implementation FeedCell

@synthesize icon;
@synthesize title;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        cellContentView = [[FeedCellContentView alloc] initWithFrame:CGRectInset(self.contentView.bounds, 0.0f, 1.0f) cell:self];
        cellContentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        cellContentView.contentMode = UIViewContentModeRedraw;
        [self.contentView addSubview:cellContentView];
        [cellContentView release];
    }
    return self;
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    
    [UIView setAnimationsEnabled:NO];
    CGSize contentSize = cellContentView.bounds.size;
    cellContentView.contentStretch = CGRectMake(225.0f / contentSize.width, 0.0f, (contentSize.width - 260.0f) / contentSize.width, 1.0f);
    [UIView setAnimationsEnabled:YES];
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:backgroundColor];
    cellContentView.backgroundColor = backgroundColor;
}

- (void)setNeedsDisplay {
    [super setNeedsDisplay];
    [cellContentView setNeedsDisplay];
}

- (void)dealloc {
	[icon release];
	[title release];
    [super dealloc];
}

@end
